package com.firmiana.jt809.netty;

import com.firmiana.jt809.util.Tools;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 809协议编码器，专用于netty server
 *
 * @author linys
 * @create 2018-08-07
 * @since 1.0.0
 */
public class JT809MessageEncoder extends MessageToByteEncoder<String> {


    public JT809MessageEncoder() {
    }

    /**
     * @param outboundMessageType
     */
    public JT809MessageEncoder(Class<? extends String> outboundMessageType) {
        super(outboundMessageType);
    }

    /**
     * @param preferDirect
     */
    public JT809MessageEncoder(boolean preferDirect) {
        super(preferDirect);
    }

    /**
     * @param outboundMessageType
     * @param preferDirect
     */
    public JT809MessageEncoder(Class<? extends String> outboundMessageType, boolean preferDirect) {
        super(outboundMessageType, preferDirect);
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception {
        byte[] content = Tools.hexString2Bytes(msg);
        out.writeBytes(content);
    }

}
